///*
// * Copyright (c) zhg2yqq Corp.
// * All Rights Reserved.
// */
//package com.zhg2yqq.wheels.security.decision;
//
//import cn.hutool.core.util.IdUtil;
//import com.zhg2yqq.wheels.security.ISecurityService;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.security.access.ConfigAttribute;
//import org.springframework.security.access.SecurityConfig;
//import org.springframework.security.web.FilterInvocation;
//import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
//import org.springframework.util.AntPathMatcher;
//
//import java.util.ArrayList;
//import java.util.Collection;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
///**
// *
// * @version zhg2yqq v1.0
// * @author zhg2yqq, 2022年11月28日
// */
//public class UrlFilterInvocationSecurityMetadataSource
//        implements FilterInvocationSecurityMetadataSource {
//    private final Logger log = LoggerFactory.getLogger(UrlFilterInvocationSecurityMetadataSource.class);
//    private final AntPathMatcher antPathMatcher = new AntPathMatcher();
//    private ISecurityService securityService;
//
//    public UrlFilterInvocationSecurityMetadataSource(ISecurityService securityService) {
//        this.securityService = securityService;
//    }
//
//    @Override
//    public Collection<ConfigAttribute> getAttributes(Object object)
//        throws IllegalArgumentException {
//        FilterInvocation invocation = (FilterInvocation) object;
//        // 获取当前请求url
//        String requestUrl = invocation.getRequestUrl();
//        String method = invocation.getRequest().getMethod();
//        log.debug(requestUrl + " " + method);
//        Map<String, Map<String, List<String>>> urlRoles = securityService
//                .listAllNeedAuthenticationUrls();
//        return urlRoles.entrySet().stream()
//                .filter(en -> antPathMatcher.match(en.getKey(), requestUrl)).map(en -> {
//                    Map<String, List<String>> ps = en.getValue();
//                    List<String> roles = new ArrayList<>();
//                    roles.addAll(ps.getOrDefault("*", new ArrayList<>()));
//                    roles.addAll(ps.getOrDefault(method.toUpperCase(), new ArrayList<>()));
//                    return roles;
//                }).flatMap(Collection::stream).distinct().map(role -> {
//                    if (role == null) {
//                        role = IdUtil.fastSimpleUUID();
//                    }
//                    return new SecurityConfig(role);
//                }).collect(Collectors.toList());
//    }
//
//    @Override
//    public Collection<ConfigAttribute> getAllConfigAttributes() {
//        return null;
//    }
//
//    @Override
//    public boolean supports(Class<?> clazz) {
//        return FilterInvocation.class.isAssignableFrom(clazz);
//    }
//
//}
